package com.rabbiter.oes.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbiter.oes.mapper.*;
import com.rabbiter.oes.service.AnswerService;
import com.rabbiter.oes.vo.AnswerVO;
import com.rabbiter.oes.vo.QuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    // 选择题
    @Autowired
    private MultiQuestionMapper multiQuestionMapper;

    // 判断题
    @Autowired
    private JudgeQuestionMapper judgeQuestionMapper;

    // 填空题
    @Autowired
    private FillQuestionMapper fillQuestionMapper;

    @Autowired
    private PaperMapper paperMapper;

    @Override
    public IPage<AnswerVO> findAll(Page<AnswerVO> page, String subjectId, String section, String question) {
        subjectId = (subjectId.equals("@") ? "" : subjectId);
        section = (section.equals("@") ? "" : section);
        question = (question.equals("@") ? "" : question);
        return answerMapper.findAll(page, subjectId, section, question);
    }

    @Override
    public QuestionVO findByIdAndType(String type, Long questionId) {
        QuestionVO questionVO = new QuestionVO();
        questionVO.setType(type);
        switch (type) {
            case "选择题":
                questionVO.setMultiQuestion(multiQuestionMapper.selectById(questionId));
                break;
            case "判断题":
                questionVO.setJudgeQuestion(judgeQuestionMapper.selectById(questionId));
                break;
            case "填空题":
                questionVO.setFillQuestion(fillQuestionMapper.selectById(questionId));
                break;
        }
        return questionVO;
    }

    @Override
    @Transactional
    public Integer deleteByIdAndType(String type, Integer questionId) {
        Integer integer = paperMapper.deleteByQuestionId(questionId);
        Integer count = 0;
        switch (type) {
            case "选择题":
                count = multiQuestionMapper.deleteById(questionId);
                break;
            case "判断题":
                count = judgeQuestionMapper.deleteById(questionId);
                break;
            case "填空题":
                count = fillQuestionMapper.deleteById(questionId);
                break;
        }
        return count;
    }
}
